package com.macro.mall.portal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.MbrMemberVideoMapper;
import com.macro.mall.model.MbrMemberVideo;
import com.macro.mall.portal.domain.MemberDetails;
import com.macro.mall.portal.service.MbrMemberVideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * MbrMemberVideo的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class MbrMemberVideoServiceImpl implements MbrMemberVideoService {

    private boolean isDebugEnabled;

    @Autowired
    private MbrMemberVideoMapper mbrMemberVideoMapper;

    @Override
    public List<MbrMemberVideo> find(MbrMemberVideo value) {
        //TODO这里可以做通过Assist做添加查询
        List<MbrMemberVideo> result = mbrMemberVideoMapper.selectMbrMemberVideo(null);
        if (isDebugEnabled) {
            log.debug("执行获取MbrMemberVideo数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public MbrMemberVideo findOne(Long id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过MbrMemberVideo的id获得MbrMemberVideo对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过MbrMemberVideo的id获得MbrMemberVideo对象-->失败:id不能为空");
        }
        MbrMemberVideo result = mbrMemberVideoMapper.selectMbrMemberVideoById(id);
        if (isDebugEnabled) {
            log.debug("执行通过MbrMemberVideo的id获得MbrMemberVideo对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(MbrMemberVideo value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将MbrMemberVideo中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
        Long userId = memberDetails.getUmsMember().getId();
        value.setMemberId(userId);
        int result = mbrMemberVideoMapper.insertNotNullMbrMemberVideo(value);
        if (isDebugEnabled) {
            log.debug("执行将MbrMemberVideo中属性值不为null的数据保存到数据库-->结果:", result);
            Asserts.fail(ResultCode.UNAUTHORIZED);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(MbrMemberVideo value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过MbrMemberVideo的id更新MbrMemberVideo中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过MbrMemberVideo的id更新MbrMemberVideo中属性不为null的数据-->失败:对象为null");
        }
        int result = mbrMemberVideoMapper.updateNotNullMbrMemberVideoById(value);
        if (isDebugEnabled) {
            log.debug("执行通过MbrMemberVideo的id更新MbrMemberVideo中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Long id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过MbrMemberVideo的id删除MbrMemberVideo-->失败:id不能为空");
            }
            Asserts.fail("执行通过MbrMemberVideo的id删除MbrMemberVideo-->失败:id不能为空");
        }
        int result = mbrMemberVideoMapper.deleteMbrMemberVideoById(id);
        if (isDebugEnabled) {
            log.debug("执行通过MbrMemberVideo的id删除MbrMemberVideo-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public void deleteByMbrMemberVideo(MbrMemberVideo mbrMemberVideo) {
        if (mbrMemberVideo == null) {
            if (isDebugEnabled) {
                log.debug("执行将MbrMemberVideo中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        Long userId = null;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            userId = memberDetails.getUmsMember().getId();
        } catch (Exception e) {
            log.error("",e);
            Asserts.fail(ResultCode.UNAUTHORIZED);
        }
        Assist assist = new Assist();
        assist.andIn("member_id", userId);
        if (!StrUtil.isEmptyIfStr(mbrMemberVideo.getTargetType())) {
            assist.andIn("target_type", mbrMemberVideo.getTargetType());
        }
        if (!StrUtil.isEmptyIfStr(mbrMemberVideo.getTargetPid())) {
            assist.andIn("target_pid", mbrMemberVideo.getTargetPid());
        }
        if (!StrUtil.isEmptyIfStr(mbrMemberVideo.getTargetType())) {
            assist.andIn("target_id", mbrMemberVideo.getTargetId());

        }

        int result = mbrMemberVideoMapper.deleteMbrMemberVideoByAssist(assist);
        if (isDebugEnabled) {
            log.debug("执行将MbrMemberVideo中属性值不为null的数据保存到数据库-->结果:", result);
        }
    }
}